package com.example.work4.adapter

import android.content.Context
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.BR
import com.example.work4.R
import com.example.work4.adapter.viewHolder.MomentViewHolder
import com.example.work4.common.ImageUtil
import com.example.work4.po.Comment
import com.example.work4.po.Moments
import com.example.work4.view.CommentDialog

class MomentsAdapter(var context:Context):BaseAdapter<Moments>() {
    override fun onBindViewHolder(holder: MomentViewHolder, position: Int) {
        val binding : ViewDataBinding = holder.dataBinding
        binding.setVariable(BR.moments, data[position])
        holder.momentId = data[position].id.toLong()
        holder.imageUri = data[position].imageUrl
        holder.momentInfo =data[position]
        val imageUtil=ImageUtil(context)
        imageUtil.GlideLoadCenterCrop(data[position].imageUrl,holder.image)
        imageUtil.GlideLoadCenterCrop(data[position].avatar,holder.avatar)
        initCommentView(data[position].commentList,holder.commentView,data[position].id,holder)
    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.item_moments
    }

    private fun initCommentView(commentList: List<Comment>?, commentView: RecyclerView,id:Int,holder: MomentViewHolder){
        var list = ArrayList<Comment>()
        if(commentList != null) {
            list = commentList as ArrayList<Comment>
        }
        commentView.layoutManager = LinearLayoutManager(context)
        val adapter = CommentsAdapter(context,id)
        adapter.data = list
        commentView.adapter = adapter
        holder.handler = adapter.mHandler
        CommentDialog.close()
    }



}